<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maxProduct = function (words) {
      var O = words.sort((a, b) => b.length - a.length);
      var S = 0;
      if (O.length < 2) { return S; }
      for (let i = 0; i < O.length; i++) {
        let A = O[i].split('');
        let B = O.filter((n, nIndex) => nIndex < i);
        for (let j = 0; j < B.length; j++) {
          let bool = !A.some(c => B[j].indexOf(c) >= 0);
          if (bool) {
            let n = B[j].length * O[i].length;
            if (n > S) { S = n }
            break;
          }
        }
      }

      return S;
    };
    console.log(maxProduct(["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]));
  </script>
</body>

</html>